<template>
  <div>
    <el-form :inline="true">
      <el-row>
        <el-col :span="5">
          <el-form-item label="公司名称：">
            <el-input v-model="selectData.companyName" placeholder="请输入公司名称" style="width: 200px"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="企业码：">
            <el-input v-model="selectData.companyCode" placeholder="请输入企业码" style="width: 240px"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="请选择状态：">
            <el-select v-model="selectData.checkStatus" @change="handleStatusChange" style="width: 200px">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="时间：">
            <el-date-picker v-model="data" type="daterange" range-separator="至" start-placeholder="开始日期"
              end-placeholder="结束日期" style="width: 240px" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="4">
          <el-button type="primary" @click="getCheckCompanyHandler">查询</el-button>
          <el-button type="primary" @click="resetHandler">重置</el-button>
        </el-col>
      </el-row>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="companyName" label="公司名称" align="center">
      </el-table-column>
      <el-table-column prop="licenseUrl" label="营业执照" align="center">
      </el-table-column>
      <el-table-column prop="enterpriseCode" label="企业码" align="center">
      </el-table-column>
      <el-table-column prop="companyIcon" label="公司图片" align="center">
      </el-table-column>
      <el-table-column prop="hrProve" label="HR在职证明" align="center">
      </el-table-column>

      <el-table-column prop="hrDO.hrName" label="HR" align="center">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center">
      </el-table-column>
      <el-table-column prop="checkStatus" label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.checkStatus == 0" style="color: gray">
            <el-button size="mini" type="primary" @click="confirmButtonHandler(scope.row)">通过</el-button>
            <el-button size="mini" type="danger" @click="rejectButtonHandler(scope.row)">不通过</el-button>
          </span>
          <span v-else-if="scope.row.checkStatus == 1" style="color: green">已通过</span>
          <span v-else-if="scope.row.checkStatus == 2" style="color: red">未通过</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="selectData.currentPage" :page-sizes="[5, 10, 15, 20]" :page-size="selectData.pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getCheckCompany, checkCompany } from "@/api/company";
export default {
  data() {
    return {
      options: [
        { value: "", label: "全部" },
        { value: 0, label: "待审核" },
        { value: 1, label: "已通过" },
        { value: 2, label: "未通过" },
      ],
      data: [],
      selectData: {
        companyName: null,
        enterpriseCode: null,
        startTime: null,
        stopTime: null,
        checkStatus: "",
      
        currentPage: 1, //当前页
        pageSize: 5, //每页大小
      },
      tableData: [],
      total: 0,
    };
  },
  methods: {
    //查询不同状态的公司
    handleStatusChange() {
      this.selectData.currentPage = 1;
    },
    //重置
    resetHandler() {
      this.selectData.companyName = null;
      this.selectData.enterpriseCode = null;
      this.selectData.checkStatus = null;
      this.selectData.startTime = null;
      this.selectData.stopTime = null;
      this.data = [];
      this.getCheckCompanyHandler();
    },

    //审核
    checkCompanyHandler(row) {
      const temp = {
        checkStatus: this.selectData.checkStatus,
        checkId: row.checkId,
      };
      checkCompany(temp).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "审核成功",
          });
        }
        this.selectData.checkStatus = "";
        this.getCheckCompanyHandler();
      });
    },
    //审核通过
    confirmButtonHandler(row) {
      this.$confirm(`此操作将通过公司名为${row.companyName},是否继续？`, {
        confirmButtonText: "确定审核通过",
        cancelButtonText: "取消审核通过",
      })
        .then(() => {
          this.selectData.checkStatus = 1;
          this.checkCompanyHandler(row);
        })
        .catch(() => { });
    },
    //审核不通过
    rejectButtonHandler(row) {
      this.$confirm(`此操作将拒绝通过公司名为${row.companyName},是否继续？`, {
        confirmButtonText: "确定拒绝通过",
        cancelButtonText: "取消拒绝通过",
      })
        .then(() => {
          this.selectData.checkStatus = 2;
          this.checkCompanyHandler(row);
        })
        .catch(() => { });
    },

    //查询所有审核记录
    getCheckCompanyHandler() {
      getCheckCompany(this.selectData)
        .then((res) => {
          console.log(res);
          
          this.tableData = res.data.list;
          this.total = res.data.total;
        })
        .catch(() => { });
    },
    handleSizeChange(val) {
      this.selectData.pageSize = val;
      this.getCheckCompanyHandler();
    },
    handleCurrentChange(val) {
      this.selectData.currentPage = val;
      this.getCheckCompanyHandler();
    },
  },
  mounted() {
    this.getCheckCompanyHandler();
  },
  watch: {
    // 监听 selectData.data 数组变化，自动拆分到 startDate 和 endDate
    data(newVal) {
      if (newVal && newVal.length === 2) {
        this.selectData.startTime = newVal[0];
        this.selectData.stopTime = newVal[1];
      } else {
        this.selectData.startTime = null;
        this.selectData.stopTime = null;
      }
    },
  },
};
</script>

<style></style>